//
//  VisitorLoginView.swift
//  sinaweibo
//
//  Created by 沈勇 on 16/5/2.
//  Copyright © 2016年 沈勇. All rights reserved.
//

import UIKit

//设置代理
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    //协议方法
    //登录
    func visitorWillLogin()
    
    //注册
    func visitorWillRegister()
    
}



class VisitorLoginView: UIView {

    //设置代理属性
    //swift中属性默认都是强引用，需要加weak修饰
    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    //登录按钮点击事件调用的方法
    func loginDidClickBtn() {
        //代理调用协议方法
        visitorDelegate?.visitorWillLogin()
        
    }
    
    //注册按钮点击事件调用的方法
    func registerClickBtn() {
        //代理调用协议方法
        visitorDelegate?.visitorWillRegister()
        
    }
    
    //MARK: 设置页面信息
    func setUIInfo(imageName: String?, title: String) {
        
        iconView.hidden = false
        tipLabel.text = title
        if imageName != nil {
            
            circleView.image = UIImage(named: imageName!)
            iconView.hidden = true
            
        }else {
            //让circleview动起来
            startAnimation()
        }
        
        
    }
    
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20
        //当动画结束，或者视图处于非活跃状态，动画不移除图层
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil )
        
    }
    
    
    
    //重写构造方法
      init() {
        super.init(frame: CGRectZero)
        
        setupUI()
        
    }

     required init?(coder aDecoder: NSCoder) {
//         fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
     }
    
    
    
    //MARK: 设置访客页面，添加页面元素
    private func setupUI() {
        
//        backgroundColor = UIColor.redColor()
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置frame布局失效
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //给圆圈添加约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -40))
        
        //给房子添加约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //给提示文案添加约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 25))
        //添加提示文案宽度约束 NotAnAttribute 没有属性 toItem： nil 没有参照对象
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 225))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //给登录按钮添加约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //给注册按钮添加约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //给背景视图图片添加约束
        addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //给背景图片添加宽高约束
        //位移枚举 || []
        //对视图宽高约束的数值：[String : NSNumber]
        addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backview]-0-|", options: [], metrics: nil , views: ["backview" : backView]))
        
//         addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backview]-(-45)-[registerBtn]", options: [], metrics: nil , views: ["backview" : backView,"registerBtn":registerBtn]))
        //上面这段代码有问题！！！
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加按钮点击事件
        loginBtn.addTarget(self, action: "loginDidClickBtn", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerClickBtn", forControlEvents: .TouchUpInside)
        
    }
    
    //MARK: 懒加载所有控件
    
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示文案
    private lazy var tipLabel: UILabel = {
       
        let l = UILabel()
        l.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(14.0)
        l.textColor = UIColor.lightGrayColor()
        l.numberOfLines = 0
        return l
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
       
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        return btn
        
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        return btn
    }()
    
    
    
    
    
    
    
    
}
